home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / gsword.c < prev    next >
C/C++ Source or Header  |  2000-05-04  |  5KB  |  224 lines

  1. /***************************************************************************
  2.   Great Swordsman
  3.  
  4.   Functions to emulate the video hardware of the machine.
  5.  
  6. ***************************************************************************/
  7.  
  8. #include "driver.h"
  9.  
  10. size_t gs_videoram_size;
  11. size_t gs_spritexy_size;
  12.  
  13. unsigned char *gs_videoram;
  14. unsigned char *gs_scrolly_ram;
  15. unsigned char *gs_spritexy_ram;
  16. unsigned char *gs_spritetile_ram;
  17. unsigned char *gs_spriteattrib_ram;
  18.  
  19. static struct osd_bitmap     *bitmap_bg;
  20. static unsigned char          *dirtybuffer;
  21. static int charbank,charpalbank;
  22. static int flipscreen;
  23.  
  24.  
  25. void gsword_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  26. {
  27.     /* sprite lookup table is not original but it is almost 98% correct */
  28.  
  29.     int sprite_lookup_table[16] = { 0x00,0x02,0x05,0x8C,0x49,0xDD,0xB7,0x06,
  30.                     0xD5,0x7A,0x85,0x8D,0x27,0x1A,0x03,0x0F };
  31.     int i;
  32.  
  33.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  34.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  35.  
  36.     for (i = 0;i < Machine->drv->total_colors;i++)
  37.     {
  38.         int bit0,bit1,bit2;
  39.  
  40.         /* red component */
  41.         bit0 = (color_prom[Machine->drv->total_colors] >> 0) & 1;
  42.         bit1 = (color_prom[Machine->drv->total_colors] >> 1) & 1;
  43.         bit2 = (color_prom[Machine->drv->total_colors] >> 2) & 1;
  44.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  45.         /* green component */
  46.         bit0 = (color_prom[Machine->drv->total_colors] >> 3) & 1;
  47.         bit1 = (color_prom[0] >> 0) & 1;
  48.         bit2 = (color_prom[0] >> 1) & 1;
  49.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  50.         /* blue component */
  51.         bit0 = 0;
  52.         bit1 = (color_prom[0] >> 2) & 1;
  53.         bit2 = (color_prom[0] >> 3) & 1;
  54.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  55.  
  56.         color_prom++;
  57.     }
  58.  
  59.     color_prom += Machine->drv->total_colors;
  60.     /* color_prom now points to the beginning of the sprite lookup table */
  61.  
  62.     /* characters */
  63.     for (i = 0;i < TOTAL_COLORS(0);i++)
  64.         COLOR(0,i) = i;
  65.  
  66.     /* sprites */
  67.     for (i = 0;i < TOTAL_COLORS(1);i++)
  68.         COLOR(1,i) = sprite_lookup_table[*(color_prom++)];
  69. }
  70.  
  71.  
  72. int gsword_vh_start(void)
  73. {
  74.     if ((dirtybuffer = malloc(gs_videoram_size)) == 0) return 1;
  75.     if ((bitmap_bg = osd_create_bitmap(Machine->drv->screen_width,2*Machine->drv->screen_height)) == 0)
  76.     {
  77.         free(dirtybuffer);
  78.         return 1;
  79.     }
  80.     memset(dirtybuffer,1,gs_videoram_size);
  81.     return 0;
  82. }
  83.  
  84. void gsword_vh_stop(void)
  85. {
  86.     free(dirtybuffer);
  87.     osd_free_bitmap(bitmap_bg);
  88. }
  89.  
  90. WRITE_HANDLER( gs_charbank_w )
  91. {
  92.     if (charbank != data)
  93.     {
  94.         charbank = data;
  95.         memset(dirtybuffer,1,gs_videoram_size);
  96.     }
  97. }
  98.  
  99. WRITE_HANDLER( gs_videoctrl_w )
  100. {
  101.     if (data & 0x8f)
  102.     {
  103.         char baf[40];
  104.         sprintf(baf,"videoctrl %02x",data);
  105.         usrintf_showmessage(baf);
  106.     }
  107.     /* bits 5-6 are char palette bank */
  108.     if (charpalbank != ((data & 0x60) >> 5))
  109.     {
  110.         charpalbank = (data & 0x60) >> 5;
  111.         memset(dirtybuffer,1,gs_videoram_size);
  112.     }
  113.     /* bit 4 is flip screen */
  114.     if (flipscreen != (data & 0x10))
  115.     {
  116.         flipscreen = data & 0x10;
  117.             memset(dirtybuffer,1,gs_videoram_size);
  118.     }
  119.  
  120.     /* bit 0 could be used but unknown */
  121.  
  122.     /* other bits unused */
  123. }
  124.  
  125. WRITE_HANDLER( gs_videoram_w )
  126. {
  127.     if (gs_videoram[offset] != data)
  128.     {
  129.         dirtybuffer[offset] = 1;
  130.         gs_videoram[offset] = data;
  131.     }
  132. }
  133.  
  134. void render_background(struct osd_bitmap *bitmap)
  135. {
  136.     int offs;
  137.  
  138.     /* for every character in the Video RAM, check if it has been modified */
  139.     /* since last time and update it accordingly. */
  140.  
  141.     for (offs = 0; offs < gs_videoram_size ;offs++)
  142.     {
  143.         if (dirtybuffer[offs])
  144.         {
  145.             int sx,sy,tile,flipx,flipy;
  146.  
  147.             dirtybuffer[offs] = 0;
  148.  
  149.             sx = offs % 32;
  150.             sy = offs / 32;
  151.             flipx = 0;
  152.             flipy = 0;
  153.  
  154.             if (flipscreen)
  155.             {
  156.                 flipx = !flipx;
  157.                 flipy = !flipy;
  158.             }
  159.  
  160.             tile = gs_videoram[offs] + ((charbank & 0x03) << 8);
  161.  
  162.             drawgfx(bitmap_bg,Machine->gfx[0],
  163.                     tile,
  164.                     ((tile & 0x3c0) >> 6) + 16 * charpalbank,
  165.                     flipx,flipy,
  166.                     8*sx,8*sy,
  167.                     0,TRANSPARENCY_NONE,0);
  168.         }
  169.     }
  170. }
  171.  
  172.  
  173. void render_sprites(struct osd_bitmap *bitmap)
  174. {
  175.     int offs;
  176.  
  177.     for (offs = 0; offs < gs_spritexy_size - 1; offs+=2)
  178.     {
  179.         int sx,sy,flipx,flipy,spritebank,tile;
  180.  
  181.         if (gs_spritexy_ram[offs]!=0xf1)
  182.         {
  183.             spritebank = 0;
  184.             tile = gs_spritetile_ram[offs];
  185.             sy = 241-gs_spritexy_ram[offs];
  186.             sx = gs_spritexy_ram[offs+1]-56;
  187.             flipx = gs_spriteattrib_ram[offs] & 0x02;
  188.             flipy = gs_spriteattrib_ram[offs] & 0x01;
  189.  
  190.             // Adjust sprites that should be far far right!
  191.             if (sx<0) sx+=256;
  192.  
  193.             // Adjuste for 32x32 tiles(#128-256)
  194.             if (tile > 127)
  195.             {
  196.                 spritebank = 1;
  197.                 tile -= 128;
  198.                 sy-=16;
  199.             }
  200.             if (flipscreen)
  201.             {
  202.                 flipx = !flipx;
  203.                 flipy = !flipy;
  204.             }
  205.             drawgfx(bitmap,Machine->gfx[1+spritebank],
  206.                     tile,
  207.                     gs_spritetile_ram[offs+1] & 0x3f,
  208.                     flipx,flipy,
  209.                     sx,sy,
  210.                     &Machine->drv->visible_area,TRANSPARENCY_COLOR, 15);
  211.         }
  212.     }
  213. }
  214.  
  215. void gsword_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  216. {
  217.     int scrollx=0, scrolly=-(*gs_scrolly_ram);
  218.  
  219.     render_background(bitmap_bg);
  220.     copyscrollbitmap(bitmap,bitmap_bg,1,&scrollx,1,&scrolly,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  221.     render_sprites(bitmap);
  222. }
  223.  
  224.